package cn.cai.controller;


import cn.cai.commonUtils.Result;
import cn.cai.pojo.EduVideo;
import cn.cai.service.IEduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author caishenao
 * @since 2021-08-17
 */
@RestController
@RequestMapping("/eduservice/video")
@Api(value = "EduVideoController",tags = "课程小节")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private IEduVideoService videoService;

    //添加课程
    @ApiOperation("添加小节")
    @PostMapping("/addVideo")
    public Result addVideo(@RequestBody
                               @ApiParam(name = "eduVideo",value = "要添加的小节")
                           EduVideo eduVideo){
        boolean save = videoService.save(eduVideo);
        return save?Result.success().data("msg","添加成功"):Result.error();
    }

    /**
     *
     *  根据小节id删除小节
     */
    @ApiOperation("根据小节id删除小节信息")
    @DeleteMapping("/deleted/{videoId}")
    public Result deletedVideo(@PathVariable("videoId") String videoId){
        boolean b = videoService.removeById(videoId);
        return b?Result.success().data("msg","删除成功"):Result.error();
    }

    /**
     * 根据小节id修改小节信息
     * @param eduVideo 要修改的对象信息，其中的id不可改变
     * @return 成功与否
     */
    @ApiOperation("根据小节id修改小节信息")
    @PutMapping("/updateVideo")
    public Result updateVideo(@RequestBody
                              @ApiParam(name = "eduVideo",value = "要修改的小节对象")
                              EduVideo eduVideo){
        //--TODO 删除视频库中的视频

        //修改，根据id
        boolean b = videoService.updateById(eduVideo);
        return b?Result.success().data("msg","修改成功"):Result.error();
    }

    @GetMapping("/getVideoById/{videoId}")
    @ApiOperation("根据传入的小节id，查询小节全部信息")
    public Result getVideoById(@PathVariable("videoId")
                                           @ApiParam(name = "videoId",value = "要查询的小节id")
                                           String videoId) {
        EduVideo eduVideo = videoService.getById(videoId);

        return eduVideo != null
                ?Result.success().data("video",eduVideo)
                :Result.error();

    }
}
